<template>
  <el-container>
    <!--左侧部分-->
    <el-aside width="200px">
      <img src="../assets/login.png">
      <p class="imgTitle">
        欢迎来到小虎云客系统
      </p>
    </el-aside>

    <!--右侧部分-->
    <el-main>

      <div class="loginTitle" style="align-items: center">请登录</div>

      <el-form ref="loginReForm" :model="user" label-width="120px" :rules="loginRules">
        <el-form-item label="账号" prop="loginAct">
          <el-input v-model="user.loginAct"/>
        </el-form-item>

        <el-form-item label="密码" prop="loginPwd">
          <el-input v-model="user.loginPwd" type="password"/>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login">登 录</el-button>
        </el-form-item>

        <el-form-item>
          <el-checkbox label="记住我" v-model="user.remeberMe" name="remeberMe"/>
        </el-form-item>
      </el-form>


    </el-main>

  </el-container>
</template>

<script>
import {doGet, doPost} from "../http/httpRequest.js";
import {getTokenName, messageTip, removeToken} from "../util/util.js";

export default {
  //组件名
  name: "LoginView",

  //定义变量
  data() {
    return {
      user: {},
      name: '',
      age: 0,
      arr: [],
      userList: [{}],
      //登录表单验证规则
      loginRules: {
        //定义用户名的验证规则
        loginAct: [
          {
            required: true,
            trigger: 'blur',
            message: "请输入账号！！"
          }
        ],
        //定义密码的验证规则
        loginPwd: [
          {
            required: true,
            trigger: 'blur',
            message: "请输入密码！！"
          },
          {
            min: 6,
            max: 12,
            message: "密码长度在6-12位之间！！"
          }
        ]
      }
    }
  },

  //页面渲染dom元素后会触发调用该函数
  mounted() {
    this.freeLogin();
  },

  //定义方法
  methods: {
    //登录方法
    login() {
      //提交前保证输入框的合法性
      this.$refs.loginReForm.validate((isValid) => {
        //isValid是验证后的结果，若是true则通过
        if (isValid) {
          //封装表单对象
          let formData = new FormData();
          formData.append("loginAct", this.user.loginAct)
          formData.append("loginPwd", this.user.loginPwd)
          formData.append("remeberMe", this.user.remeberMe)

          //验证通过,可以提交
          doPost("/api/login", formData).then((resp) => {
            console.log(resp);
            if (resp.data.code === 200) {
              //提示登录成功
              messageTip("登陆成功！！！", "success");

              //删除历史localStorage和sessionStorage中存储的token
              removeToken();

              //存储前端的jwt
              if (this.user.remeberMe === true) {
                window.localStorage.setItem(getTokenName(), resp.data.data)
              } else {
                window.sessionStorage.setItem(getTokenName(), resp.data.data)
              }
              //跳转到系统主页面
              window.location.href = "/dashboard";
            } else {
              //提示登录失败
              messageTip("登录失败!用户名或密码错误！！", "error");
            }
          })
        }
      })
    },

    //免登录方法(自动登录)
    freeLogin() {
      let token = window.localStorage.getItem(getTokenName());
      if (token) { //代表有值，选择了记住我
        doGet("/api/login/free", {}).then(resp => {
          if (resp.data.code == 200) {
            //token验证成功，可以免登录
            window.location.href = "/dashboard";
          }
        })
      }
    }
  }
}
</script>

<style scoped>
.el-aside {
  background-color: #cbe7fc;
  width: 40%;
  text-align: center;
}

.el-main {
  background-color: #FABDCC;
  height: calc(100vh);
}

img {
  width: 100%;
  height: auto;
}

.imgTitle {
  font-size: 20px;
  font-weight: bold;
  color: #000000;
}

.el-form {
  width: 60%;
  margin: auto;
}

.el-button {
  width: 100%;
  margin: auto;
}

.loginTitle {
  text-align: center;
  margin-top: 100px;
  margin-bottom: 15px;
  font-weight: bold;
}
</style>